import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { map } from 'rxjs/operators';
import { SoftwareService, SoftwareStatus } from '../../services/software.service';
import { MySelection } from 'app/modules/base/selection';
import { ZoneService } from 'app/modules/zone/services/zone.service';
import { SweetAlertService } from 'app/modules/share/services/sweet-alert.service';

@Component({
  selector: 'm-status-table',
  templateUrl: './status-table.component.html',
  styleUrls: ['./status-table.component.scss'],
})
export class StatusTableComponent implements OnInit {
  constructor(
    private route: ActivatedRoute,
    private sweet: SweetAlertService,
    private softwareService: SoftwareService,
  ) {}

  softStatus$ = this.route.queryParamMap.pipe(
    map(query => Number(query.get('id'))),
    map(id => this.softwareService.softStatus(id)),
  );
  statusSelection = new MySelection<SoftwareStatus>('id');

  ngOnInit() {}

  get softwareID() {
    return Number(this.route.snapshot.queryParamMap.get('id'));
  }

  async delete(...zid: number[]) {
    if ((await this.sweet.confirm('确定要删除吗？')).dismiss) {
      return;
    }
    if (!zid.length) {
      zid = this.statusSelection.keys();
    }
    await this.softwareService.deleteInZone(this.softwareID, zid);
  }
}
